package com.gx.smart.construction.ui.house.fragment

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.construction.R
import com.gx.smart.construction.databinding.SmartHomeRecordFragmentV4Binding
import com.gx.smart.construction.ui.house.adapter.RecordItemPagingDataAdapter
import com.gx.smart.construction.ui.house.model.RecordType
import com.gx.smart.construction.ui.house.viewmodel.SmartHomeRecordViewModel
import com.mylhyl.circledialog.CircleDialog
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class SmartHomeRecordFragmentV4 : BaseMVVMFragmentV2<SmartHomeRecordViewModel>(),
    OnRefreshListener {
    private lateinit var adapter: RecordItemPagingDataAdapter
    private lateinit var dataBinding: SmartHomeRecordFragmentV4Binding
    private val items: MutableList<RecordType> = ArrayList()
    private var type = 3
    override fun onBindLayout(): Int {
        return R.layout.smart_home_record_fragment_v4
    }

    override fun getToolbarTitle(): String {
        return "添加记录"
    }

    override fun onBindViewModel(): Class<SmartHomeRecordViewModel> =
        SmartHomeRecordViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        dataBinding = DataBindingUtil.inflate(layoutInflater, onBindLayout(), root, true)
        with(dataBinding) {
            lifecycleOwner = viewLifecycleOwner
            this.smartHomeRecordFragment = this@SmartHomeRecordFragmentV4
            smartHomeRecordViewModel = viewModel
            refreshLayout.setOnRefreshListener(this@SmartHomeRecordFragmentV4)
            adapter = RecordItemPagingDataAdapter(requireActivity())
            recyclerView.adapter = adapter
            refreshLayout.setEnableLoadMore(false)
        }

        val divider = DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL)

        @Suppress("DEPRECATION")
        @SuppressLint("UseCompatLoadingForDrawables")
        val drawable =
            resources.getDrawable(R.drawable.shape_list_item_divider_line)
        divider.setDrawable(drawable)
        dataBinding.recyclerView.addItemDecoration(divider)
    }

    override fun initData() {
        val item1 = RecordType()
        item1.type = 3
        item1.content = "全部"
        items.add(item1)
        val item2 = RecordType()
        item2.type = 1
        item2.content = "未绑定"
        items.add(item2)
        val item3 = RecordType()
        item3.type = 2
        item3.content = "已绑定"
        items.add(item3)
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        viewModel.refresh()
    }

    fun selectRoomType(v: View?) {
        CircleDialog.Builder()
            .setItems(items) { _: AdapterView<*>?, _: View?, position: Int, _: Long ->
                val item = items[position]
                type = item.type
                viewModel.setSelectedType(item.content)
                dataBinding.refreshLayout.autoRefresh()
                true
            }
            .setNegative("取消", null)
            .show(requireActivity().supportFragmentManager)
    }

    override fun initObserver() {
        super.initObserver()
        lifecycleScope.launch {
            viewModel.roomInfoList.collectLatest {
                adapter.submitData(it)
            }
        }
    }
}